// 重写数组方法
// 1. 获取数组原来的方法
let oldArrayProtoMethods = Array.prototype

// 2. 继承
export let arrayMethods = Object.create(oldArrayProtoMethods)

// 劫持

let methods = ['push', 'pop', 'unshift', 'shift', 'splice']

methods.forEach((item) => {
  arrayMethods[item] = function (...args) {
    // console.log('劫持数组')
    let result = oldArrayProtoMethods[item].apply(this, args)
    // 追加对象
    let inserted
    switch (item) {
      case 'push':
      case 'unshift':
        inserted = args
        break
      case 'splice':
        inserted = args.splice(2)
    }
    // console.log(inserted)
    let ob = this.__ob__
    if (inserted) {
      ob.observeArray(inserted) // 对我们添加的对象进行劫持
    }
    return result
  }
})
